#!/bin/bash
# 一键安装所有软件

# 检查是否以root用户运行
if [ $EUID -ne 0 ]; then
    echo "请以root用户运行此脚本"
    exit 1
fi

# 脚本执行开始时间
start_time=$(date +%s)

# 定义要安装的组件及顺序
scripts=(
    "01-setup_aliyun_repo.sh"       # 1. 配置阿里云源
    "02-install_jdk.sh"             # 2. 安装JDK
    "03-install_maven.sh"           # 3. 安装Maven
    "04-install_tomcat.sh"          # 4. 安装Tomcat
    "05-install_redis.sh"           # 5. 安装Redis
    "06-install_node.sh"            # 6. 安装Node.js
    "07-install_nginx.sh"           # 7. 安装Nginx
    "08-install_mysql.sh"           # 8. 安装MySQL
    "09-install_mongodb.sh"         # 9. 安装MongoDB
    "10-install_php.sh"             # 10. 安装PHP
    "11-install_golang.sh"          # 11. 安装Golang
    "12-install_grafana.sh"         # 12. 安装Grafana
)

# 显示安装计划
echo "===== 安装计划 ====="
for i in "${!scripts[@]}"; do
    echo "$((i+1)). ${scripts[$i]}"
done
echo "===================="

read -p "即将开始安装所有组件，是否继续(y/n)?" answer
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
    echo "取消安装"
    exit 0
fi

# 执行安装
for script in "${scripts[@]}"; do
    echo "======================================"
    echo "开始执行: $script"
    echo "======================================"
    
    # 检查脚本是否存在
    if [ ! -f "$script" ]; then
        echo "错误: 脚本 $script 不存在"
        read -p "是否继续安装其他组件(y/n)?" cont
        if [ "$cont" != "y" ] && [ "$cont" != "Y" ]; then
            exit 1
        else
            continue
        fi
    fi
    
    # 执行脚本
    chmod +x "$script"
    ./"$script"
    
    # 检查执行结果
    if [ $? -ne 0 ]; then
        echo "错误: $script 执行失败"
        read -p "是否继续安装其他组件(y/n)?" cont
        if [ "$cont" != "y" ] && [ "$cont" != "Y" ]; then
            exit 1
        fi
    fi
    
    echo ""
    echo ""
done

# 计算总耗时
end_time=$(date +%s)
cost_time=$((end_time - start_time))
hours=$((cost_time / 3600))
minutes=$(( (cost_time % 3600) / 60 ))
seconds=$((cost_time % 60))

echo "======================================"
echo "所有组件安装完成!"
echo "总耗时: ${hours}小时${minutes}分钟${seconds}秒"
echo "======================================"
